import json


class create_collections:
    def __init__(self,response,client_host,client_port):

        self.response=response
        self.host=client_host
        self.port=client_port

    def create_coll(self):
        
        collections_list=json.loads(self.response["json_encoded_response"])["column_1"]
        
        collecs_dict={}
        collec_list=[]

        for collection in collections_list:
                    
                    
                    collec_dict={"id":collection,"type":"Collection","dataSource":collection,"stac_version":"1.0.0"}
                    
                    
                    collec_dict["links"]=[{
                                "rel": "items",
                                "type": "application/geo+json",
                                "href": f"http://{self.host}:{self.port}/collections/{collection}/items"
                            },
                            {
                                "rel": "parent",
                                "type": "application/json",
                                "href": f"http://{self.host}:{self.port}/"
                            },
                            {
                                "rel": "root",
                                "type": "application/json",
                                "href": f"http://{self.host}:{self.port}/"
                            },
                            {
                                "rel": "self",
                                "type": "application/json",
                                "href": f"http://{self.host}:{self.port}/collections/{collection}"
                            }]
                    collec_list.append(collec_dict)
        collecs_dict["collections"]=collec_list
        collecs_dict["links"]=[{
                    "rel": "root",
                    "type": "application/json",
                    "href": f"http://{self.host}:{self.port}/"
                },
                {
                    "rel": "parent",
                    "type": "application/json",
                    "href": f"http://{self.host}:{self.port}/"
                },
                {
                    "rel": "self",
                    "type": "application/json",
                    "href": f"http://{self.host}:{self.port}/collections"
                }]
        return collecs_dict
    
    def create_collid(self):
            

        collec_dict={"id":self.response,"type":"Collection","dataSource":self.response,"stac_version":"1.0.0"}

        
        collec_dict["links"]=[{
                    "rel": "items",
                    "type": "application/geo+json",
                    "href": f"http://{self.host}:{self.port}/collections/{self.response}/items"
                },
                {
                    "rel": "parent",
                    "type": "application/json",
                    "href": f"http://{self.host}:{self.port}/"
                },
                {
                    "rel": "root",
                    "type": "application/json",
                    "href": f"http://{self.host}:{self.port}/"
                },
                {
                    "rel": "self",
                    "type": "application/json",
                    "href": f"http://{self.host}:{self.port}/collections/{self.response}"
                }]
                    
        
        return collec_dict
    
    
    def create_root(self):
         
        roots={
            "type": "Catalog",
            "id": "stac-fastapi",
            "title": "stac-fastapi",
            "description": "stac-fastapi",
            "stac_version": "1.0.0",
            "conformsTo": [
                "http://www.opengis.net/spec/ogcapi-features-3/1.0/conf/features-filter",
                "https://api.stacspec.org/v1.0.0-rc.1/item-search#query",
                "https://api.stacspec.org/v1.0.0-rc.1/item-search#filter:cql-text",
                "http://www.opengis.net/spec/ogcapi-features-1/1.0/conf/geojson",
                "http://www.opengis.net/spec/ogcapi-features-4/1.0/conf/simpletx",
                "https://api.stacspec.org/v1.0.0-rc.1/item-search#fields",
                "https://api.stacspec.org/v1.0.0-rc.1/item-search#context",
                "https://api.stacspec.org/v1.0.0-rc.1/ogcapi-features",
                "http://www.opengis.net/spec/ogcapi-features-1/1.0/conf/oas30",
                "https://api.stacspec.org/v1.0.0-rc.1/item-search#sort",
                "https://api.stacspec.org/v1.0.0-rc.1/item-search#filter:basic-cql",
                "http://www.opengis.net/spec/ogcapi-features-3/1.0/conf/filter",
                "http://www.opengis.net/spec/ogcapi-features-1/1.0/conf/core",
                "https://api.stacspec.org/v1.0.0-rc.1/item-search#filter",
                "https://api.stacspec.org/v1.0.0-rc.1/collections",
                "https://api.stacspec.org/v1.0.0-rc.1/core",
                "https://api.stacspec.org/v1.0.0-rc.1/ogcapi-features/extensions/transaction",
                "https://api.stacspec.org/v1.0.0-rc.1/item-search"
            ]
         }

        link= [{
            "rel": "self",
            "type": "application/json",
            "href": f"http://{self.host}:{self.port}/"
        },
        {
            "rel": "root",
            "type": "application/json",
            "href": f"http://{self.host}:{self.port}/"
        },
        {
            "rel": "data",
            "type": "application/json",
            "href": f"http://{self.host}:{self.port}/collections"
        },
        {
            "rel": "conformance",
            "type": "application/json",
            "title": "STAC/WFS3 conformance classes implemented by this server",
            "href": f"http://{self.host}:{self.port}/conformance"
        },
        {
            "rel": "search",
            "type": "application/geo+json",
            "title": "STAC search",
            "href": f"http://{self.host}:{self.port}/search",
            "method": "GET"
        },
        {
            "rel": "search",
            "type": "application/geo+json",
            "title": "STAC search",
            "href": f"http://{self.host}:{self.port}/search",
            "method": "POST"
        }]

        collections_list=json.loads(self.response["json_encoded_response"])["column_1"]
        
       
        for collection in collections_list:
                    
                   

                    root_dict={
            "rel": "child",
            "type": "application/json",
            "title": collection,
            "href": f"http://{self.host}:{self.port}/collections/{collection}"
        }
                    link.append(root_dict)
  
        link.append(
        {
            "rel": "service-doc",
            "type": "text/html",
            "title": "OpenAPI service documentation",
            "href": f"http://{self.host}:{self.port}/stac_docs"
        })

        roots["links"]=link
        roots["stac_extensions"]=[
        "https://raw.githubusercontent.com/radiantearth/stac-api-spec/v1.0.0-rc.1/fragments/context/json-schema/schema.json"
    ]
        
        return roots